import {HttpProvider, AjaxOption} from '../../common/http'
import {Injectable} from '@angular/core';
import {API} from "../../common/api";
import {Helper} from "../../common/Helper";

@Injectable()
export class ProductProvider {

  constructor(public http: HttpProvider,
              public helper: Helper) {
    console.log('Hello ProductProvider Provider');
  }

  getList() {
    return this.http.requestWithoutToken({
      type: 'GET',
      url: API.NO_LOGIN_PRODUCT_LIST,
    }).catch(err => {
      this.http.toastMsg(err.msg)
    })
  }

  getDetail(options:AjaxOption) {
    if(options.loading!==false) this.helper.loading('加载中..')
    return this.http.requestWithoutToken({
      type: 'GET',
      url: API.NO_LOGIN_PRODUCT_DETAIL,
      data: {
        loanId: options.data.id
      }
    }).then((d)=>{
      this.helper.stopLoading()
      options.success && options.success(d)
    }).catch(err => {
      this.helper.stopLoading()
      this.http.toastMsg(err.msg)
    })
  }

  getRecordList(options:AjaxOption) {
    return this.http.requestWithoutToken({
      type: 'GET',
      url: API.NO_LOGIN_INVESTMEMT,
      data: {
        id: options.data.id
      }
    }).then((d)=>{
      options.success && options.success(d)
    }).catch(err => {
      this.http.toastMsg(err.msg)
    })
  }

  investBid(options:AjaxOption){
    this.helper.loading("请稍后..")
    return this.http.ajax({
      type: "POST",
      url: API.INVEST_BID,
      options: options
    })
  }

}
